﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 得到DataGrid,GridView,DataList被钩选的行的ID
    /// </summary>
    public static class GetSelectedRow
    {
        #region 得到GridView被钩选的行的ID

        /// <summary>
        /// 得到GridView被钩选的行的ID
        /// </summary>
        /// <param name="gvList"></param>
        /// <returns></returns>
        public static string GetIDs(GridView gvList)
        {
            MyStringBuilder winner = new MyStringBuilder();

            foreach (GridViewRow gvr in gvList.Rows)
            {
                CheckBox cbSelectId = null;

                foreach (Control c in gvr.Cells[0].Controls)
                {
                    if (c is CheckBox)
                    {
                        cbSelectId = c as CheckBox;
                        break;
                    }
                }

                if (cbSelectId == null)
                {
                    throw new ArgumentNullException("第一列没有CheckBox");
                }
                if (cbSelectId.Checked)
                {
                    winner.Append(gvList.DataKeys[gvr.RowIndex].Value.ToString());
                    winner.Append(",");
                }
            }
            winner.TrimEnd();
            return winner.ToString();
        }

        /// <summary>
        /// 得到GridView被钩选的行的ID
        /// </summary>
        /// <param name="gvList"></param>
        /// <param name="cells"></param>
        /// <param name="chkName"></param>
        /// <returns></returns>
        /// <remarks>ID列不能是被隐藏的</remarks>
        public static string GetIDs(GridView gvList, int cells, string chkName)
        {
            List<string> list = new List<string>();
            foreach (GridViewRow gvr in gvList.Rows)
            {
                CheckBox cbSelectID = (CheckBox) gvr.FindControl(chkName);
                if (cbSelectID.Checked)
                {
                    list.Add(gvr.Cells[cells].Text.Trim());
                }
            }
            if (list.Count == 0)
            {
                return null;
            }
            return string.Join(",", list.ToArray());
        }

        #endregion

        #region 得到DataList被钩选的行的ID

        /// <summary>
        /// 得到DataList被钩选的行的ID
        /// </summary>
        /// <param name="dlList"></param>
        /// <returns></returns>
        public static string GetIDs(DataList dlList)
        {
            return GetIDs(dlList, "CB_SelectID");
        }

        /// <summary>
        /// 得到DataList被钩选的行的ID
        /// </summary>
        /// <param name="dlList"></param>
        /// <param name="chkName"></param>
        /// <returns></returns>
        public static string GetIDs(DataList dlList, string chkName)
        {
            List<string> list = new List<string>();
            foreach (DataListItem dli in dlList.Items)
            {
                CheckBox cbSelectID = (CheckBox) dli.FindControl(chkName);
                if (cbSelectID.Checked)
                {
                    HiddenField id = (HiddenField) dli.FindControl("HF_ID");
                    list.Add(id.Value);
                }
            }
            if (list.Count == 0)
            {
                return null;
            }
            return string.Join(",", list.ToArray());
        }

        #endregion

        #region 得到Repeater被钩选的行的ID

        /// <summary>
        /// 得到Repeater被钩选的行的ID
        /// </summary>
        /// <param name="rList"></param>
        /// <returns></returns>
        public static string GetIDs(Repeater rList)
        {
            return GetIDs(rList, "CB_SelectID");
        }

        /// <summary>
        /// 得到Repeater被钩选的行的ID
        /// </summary>
        /// <param name="rList"></param>
        /// <param name="chkName"></param>
        /// <returns></returns>
        public static string GetIDs(Repeater rList, string chkName)
        {
            MyStringBuilder sbIDs = new MyStringBuilder();
            foreach (RepeaterItem dli in rList.Items)
            {
                CheckBox cbSelectID = (CheckBox) dli.FindControl(chkName);
                if (cbSelectID.Checked)
                {
                    HiddenField id = (HiddenField) dli.FindControl("HF_ID");

                    sbIDs.Append(id.Value);
                    sbIDs.Append(",");
                }
            }
            sbIDs.TrimEnd();

            if (sbIDs.ToString().Length > 0)
            {
                return sbIDs.ToString();
            }
            return null;
        }

        #endregion

        #region 得到ListBox被钩选的行的ID

        /// <summary>
        /// 得到ListBox被钩选的行的ID
        /// </summary>
        /// <param name="lbList"></param>
        /// <returns></returns>
        public static string GetIDs(ListControl lbList)
        {
            MyStringBuilder winner = new MyStringBuilder();

            foreach (ListItem li in lbList.Items.Cast<ListItem>().Where(li => li.Selected))
            {
                winner.Append(li.Value);
                winner.Append(",");
            }
            winner.TrimEnd();
            return winner.ToString();
        }

        #endregion
    }
}